package exam.controller;

import com.alibaba.fastjson.JSONObject;
import exam.locadomain.Title;
import exam.locadomain.server.TitleServer;
import exam.localdata.LocalPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/3 16:48
 * @since JDK 1.8
 */
@RestController
@Api(tags = {"文章服务模块"})
public class TitleController {

    @Autowired
    private TitleServer titleServer;

    @PutMapping("addtitle")
    @ApiOperation("新建文章")
    public JSONObject addTitleController(@ApiParam("标题")@RequestParam("title")String title,
                                         @ApiParam("备注")@RequestParam("note")String note,
                                         @ApiParam("文章内容")@RequestParam("content")String content){
        titleServer.addTitleContent(title,note,content,"用户账号","用户id");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","success");
        return jsonObject;
    }

    @GetMapping("getpage")
    @ApiOperation("文章列表")
    public JSONObject titleListController(@ApiParam("页码")@RequestParam(value = "page",defaultValue = "1")Integer page,
                                          @ApiParam("条数")@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                          @ApiParam("文章名称")@RequestParam(value = "title",required = false)String title,
                                          @ApiParam("文章备注")@RequestParam(value = "note",required = false)String note){
        JSONObject jsonObject=new JSONObject();
        LocalPage<Title> titleLocalPage=titleServer.getTitleListPage(page,pageSize,title,note);
        jsonObject.put("code",0);
        jsonObject.put("msg","success");
        jsonObject.put("data",titleLocalPage);
        return jsonObject;
    }

    @GetMapping("getcontentById")
    @ApiOperation("获取文章列表通过id")
    public JSONObject getContentByIdController(@ApiParam("文章uuid")@RequestParam("uuid")String uuid){
        JSONObject jsonObject=new JSONObject();
        if(!StringUtils.hasText(uuid)){
            jsonObject.put("code",1);
            jsonObject.put("msg","uuid不得为空");
            return jsonObject;
        }
        Map<String, Object> objectMap=titleServer.getContentById(uuid);
        jsonObject.put("code",0);
        jsonObject.put("data",objectMap);
        return jsonObject;
    }


}
